package com.ruoyi.web.controller.news.api;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.news.EvaluationPersonInfo;
import com.ruoyi.system.domain.news.query.EvaluationPersonInfoQuery;
import com.ruoyi.system.domain.news.vo.EvaluationPersonInfoVo;
import com.ruoyi.system.service.news.EvaluationPersonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/news/evaluationPersonInfo")
@Api(value = "evaluationPersonInfoApiController", tags  = "机构人员信息Api")
public class EvaluationPersonInfoApiController extends BaseController {

	@Resource
	private EvaluationPersonInfoService evaluationPersonInfoService;

	@ApiOperation(value = "获取单个机构人员信息", notes = "获取单个机构人员信息")
	@GetMapping("/getById")
	public R<EvaluationPersonInfo> getById(@RequestParam(value = "id") Long id) {
		EvaluationPersonInfoQuery evaluationPersonInfoQuery = new EvaluationPersonInfoQuery();
		evaluationPersonInfoQuery.setId(id);
		List<EvaluationPersonInfoVo> evaluationPersonInfoList = evaluationPersonInfoService.getEvaluationPersonInfoList(evaluationPersonInfoQuery);
		if (evaluationPersonInfoList!=null&&evaluationPersonInfoList.size()>0) {
			EvaluationPersonInfoVo evaluationPersonInfoVo = evaluationPersonInfoList.get(0);
			evaluationPersonInfoVo.setIdNumber(null);
			return R.ok(evaluationPersonInfoVo);
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取机构人员信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<EvaluationPersonInfoVo>> list(@RequestBody EvaluationPersonInfoQuery evaluationPersonInfoQuery) {
        evaluationPersonInfoQuery.setIsEnable("1");
		List<EvaluationPersonInfoVo> evaluationPersonInfoList = evaluationPersonInfoService.getEvaluationPersonInfoList(evaluationPersonInfoQuery);
		if (!evaluationPersonInfoList.isEmpty()) {
			for (EvaluationPersonInfoVo evaluationPersonInfoVo:evaluationPersonInfoList) {
				evaluationPersonInfoVo.setIdNumber(null);
			}
		}
		return R.ok(new PageInfo<>(evaluationPersonInfoList));
	}

	@ApiOperation(value = "导入机构人员模板下载")
	@GetMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response)
	{
		ExcelUtil<EvaluationPersonInfo> util = new ExcelUtil<EvaluationPersonInfo>(EvaluationPersonInfo.class);
		util.importTemplateExcel(response, "机构人员数据");
	}

	@ApiOperation(value = "导入机构人员数据")
	@PostMapping("/importData")
	public R importData(MultipartFile file, String agencyId) throws Exception
	{
		ExcelUtil<EvaluationPersonInfo> util = new ExcelUtil<EvaluationPersonInfo>(EvaluationPersonInfo.class);
		List<EvaluationPersonInfo> evaluationPersonInfoList = util.importExcel(file.getInputStream());
		if (evaluationPersonInfoList.isEmpty()) {
			return R.fail("请填写数据");
		}
		for (EvaluationPersonInfo evaluationPersonInfo:evaluationPersonInfoList) {
			evaluationPersonInfo.setAgencyId(agencyId);
		}
		evaluationPersonInfoService.saveBatch(evaluationPersonInfoList);
		return R.ok("导入成功");
	}
}
